/*
 reading GWS 2.4G receiver
 2 Aug: to verify 3.3V GWS abvle to drive pulseIn on 3V arduino
 Level[4] as read in ppm in us


 */
int ch1 = 2;
int ch2 = 3;
int ch3 = 4;
int ch4 = 5;
int Level[4] = { 0, 0, 0, 0 };
int D1, D2, D3, D4;
int v;
void setup() {
	pinMode(ch1, INPUT);
	pinMode(ch2, INPUT);
	pinMode(ch3, INPUT);
	pinMode(ch4, INPUT);
	Serial.begin(9600);
}

void loop() {
	Level[0] = pulseIn(ch1, HIGH);
	Level[1] = pulseIn(ch2, HIGH);
	Level[2] = pulseIn(ch3, HIGH);
	Level[3] = pulseIn(ch4, HIGH);
	D1 = MStoBin(Level[0]);
	D2 = MStoBin(Level[1]);
	D3 = MStoBin(Level[2]);
	D4 = MStoBin(Level[3]);

	v = ((D1 & B00000011) << 6) + ((D2 & B00000011) << 4) + ((D3 & B00000011)
			<< 2) + (D4 & B00000011);
	Serial.print("v:");
	Serial.println(v, BIN);
	Serial.print("ch1 = ");
	Serial.println(D1);
	Serial.print("ch2 = ");
	Serial.println(D2);
	Serial.print("ch3 = ");
	Serial.println(D3);
	Serial.print("ch4 = ");
	Serial.println(D4);
	Serial.print("v:");
	Serial.println(v, BYTE);
	delay(1000);
}
int MStoBin(int pluse) {
	if (pluse > 1500) {
		if (pluse > 1750)
			return 3;
		else
			return 2;
	}//end if of pluse>1500
	else {
		if (pluse < 1250)
			return 0;
		else
			return 1;
	}

}// end of MStoBin
